package sedev.struts.portlet;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import sedev.shopping.business.HinhThucDichVuService;
import sedev.shopping.entities.HinhThucDichVu;
import sedev.shopping.entities.ListHinhThucDichVu;
import sedev.shopping.util.SedevUtil;

import com.liferay.portal.util.PortalUtil;
import com.opensymphony.xwork2.ActionSupport;

public class BusinessTypeAction extends ActionSupport implements
		ServletRequestAware {
	private static final long serialVersionUID = 1L;
	private HttpServletRequest request;
	private Long ownerId;
		
	private int preExecute() {
		if(SedevUtil.isAdministrator(this.ownerId) == false){
			return -1;
		}
		return 0;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
		this.ownerId = PortalUtil.getUserId(this.request);		
	}

	@Override
	public String execute() throws Exception {
		if(this.preExecute() < 0){
			return "denied";
		}
		ListHinhThucDichVu listdata = HinhThucDichVuService.getListDataByAll(SedevUtil.Order.DESC);
		this.request.setAttribute("listloaidichvu", listdata);
		return ActionSupport.SUCCESS;
	}

	public String post() throws Exception {
		if(this.preExecute() < 0){
			return "denied";
		}
		return ActionSupport.SUCCESS;
	}

	public String edit() throws Exception {
		if(this.preExecute() < 0){
			return "denied";
		}
		HinhThucDichVu data = new HinhThucDichVu();
		try {
			int id = Integer.valueOf(this.request.getParameter("businesstypeid"));
			data = HinhThucDichVuService.getData(id);
			if (data != null) {
				this.request.setAttribute("hinhthucdichvu", data);				
				return ActionSupport.SUCCESS;
			} else {
				this.request.setAttribute("errorCode", -1);
			}
		} catch (Exception ex) {
			this.request.setAttribute("errorCode", -1);
		}
		// return index page		
		return ActionSupport.ERROR;
	}

	public String add() throws Exception {
		if(this.preExecute() < 0){
			return "denied";
		}
		HinhThucDichVu data = new HinhThucDichVu();
		try {
			if (this.initData(data)) {
				if (HinhThucDichVuService.addData(data)) {
					this.request.setAttribute("errorCode", 1);
					this.request.setAttribute("hinhthucdichvu", data);					
					return ActionSupport.SUCCESS;
				}
			} else {
				this.request.setAttribute("errorCode", -1);
			}
		} catch (Exception ex) {
			this.request.setAttribute("errorCode", -1);
		}
		// return post page		
		return ActionSupport.ERROR;
	}

	public String update() throws Exception {
		if(this.preExecute() < 0){
			return "denied";
		}
		HinhThucDichVu data = new HinhThucDichVu();
		try {
			data.setId(Integer.valueOf(this.request.getParameter("id")));
			if (data.getId() != null) {
				if (this.initData(data)) {
					if (HinhThucDichVuService.updateData(data)) {
						this.request.setAttribute("errorCode", 2);
					} else {
						this.request.setAttribute("errorCode", -1);
					}
				} else {
					this.request.setAttribute("errorCode", -1);
				}
				this.request.setAttribute("hinhthucdichvu", data);				
				return ActionSupport.SUCCESS;
			} else {
				this.request.setAttribute("errorCode", -1);
			}
		} catch (Exception ex) {
			this.request.setAttribute("errorCode", -1);
		}
		// return edit page		
		return ActionSupport.ERROR;
	}

	public String remove() throws Exception {
		if(this.preExecute() < 0){
			return "denied";
		}
		int id = Integer.valueOf(this.request.getParameter("id"));
		if(HinhThucDichVuService.removeData(id)){
			this.request.setAttribute("errorCode", 1);
		}else{
			this.request.setAttribute("errorCode", -1);
		}
		ListHinhThucDichVu listdata = HinhThucDichVuService.getListDataByAll(SedevUtil.Order.DESC);
		this.request.setAttribute("listloaidichvu", listdata);		
		return ActionSupport.SUCCESS;
	}

	private boolean initData(HinhThucDichVu data) {
		try {
			data.setTen(this.request.getParameter("title"));
		} catch (Exception ex) {
			data.setTen("");
		}		
		return true;
	}
}
